import axios from 'axios';
import jsonp from '@/common/js/jsonp';
import { commonParams, options } from './config';

export function getRecommend(){
	const url = 'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg';
	const data = Object.assign({}, commonParams, {
		platform: 'h5',
		uin: 0,
		needNewCode: 1
	});
	for(let k in data){
		delete data[k];
	}
	return jsonp(url, data, options);
}

export function getSingers() {
	const url = '/api/getSingerList';
	const params = {
		offset: 0,
		limit: 100
	};
	return axios.get(url, { params }).then((res) => {
		return Promise.resolve(res.data);
	});
}

export function getDiscList() {
	const url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg';
	const data = Object.assign({}, commonParams, {
		platform: 'yqq',
		hostUin: 0,
		sin: 0,
		ein: 29,
		sortId: 5,
		needNewCode: 0,
		categoryId: 10000000,
		rnd: Math.random(),
		format: 'json'
	});
	return axios.get(url, {
		params: data
	}).then((res) => {
		return Promise.resolve(res.data);
	})
}

export function getHotKey() {
	const url = 'https://c.y.qq.com/splcloud/fcgi-bin/gethotkey.fcg';
	const data = Object.assign({}, commonParams, {
		platform: 'h5',
		needNewCode: 1
	});
	for(let k in data){
		delete data[k];
	}
	return jsonp(url, data, options);
}

function getRandomVal(prefix = '') {
  return prefix + (Math.random() + '').replace('0.', '')
}

// 下面的josnp方式失败了，腾讯服务器那里禁止跨域访问
export function getSearchList(query, page, zhida) {
	const url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
	const data = Object.assign({}, commonParams, {
	    w: query,
	    p: page,
	    n: 20,
	    zhidaqu: 1,
	    catZhida: zhida ? 1 : 0,
	    t: 0,
	    flag: 1,
	    catZhida: 1,
	    ie: 'utf - 8',
	    sem: 1,
	    aggr: 0,
	    perpage: 20,
	    remoteplace: 'txt.mqq.all',
	    uid: 0,
	    needNewCode: 1,
	    platform: 'h5',
	    format: 'json'
	});
	return jsonp(url, data, options);
}
